Skip to content

London | 26-ITP-Sep | Hanna Bohlin | Sprint 1 | formatAs12HourClock - #1628

Open
habohlin wants to merge 17 commits into
CodeYourFuture:mainfrom
habohlin:Sprint-1-Coursework
Open

habohlin wants to merge 17 commits into
CodeYourFuture:mainfrom
habohlin:Sprint-1-Coursework

Conversation

@habohlin

@habohlin habohlin commented Sep 28, 2026 •

Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Task code

CYF-1197

Changelist

I wrote tests for the function for as many edge cases I could think of, and corrected the function to pass the tests when the tests failed.

@github-actions

This comment has been minimized.

@habohlin habohlin added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Sep 28, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Sep 28, 2026
@habohlin habohlin added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Sep 28, 2026
@illicitonion illicitonion added Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Sep 28, 2026

@illicitonion illicitonion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works and generally looks good, but I left a few comments to think about :)

Comment on lines +14 to +23
test("can format afternoon time with minutes other than 00", () =>
assert.equal(formatAs12HourClock("15:45"), "03:45 pm"));

test("can format morning time with complex minutes", () =>
assert.equal(formatAs12HourClock("08:25"), "08:25 am"));

test("can format early noon complex minutes", () =>
assert.equal(formatAs12HourClock("12:17"), "12:17 pm"));

test("can format between midnight and 1 am", () =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are really good tests but you're using inconsistent terminology here - sometimes you're saying "minutes other than 00" and other times "complex minutes". By using different terms it makes me as a reader wonder whether they have different meanings. If you mean the same thing, I'd recommend using the same term.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent now, thanks.

test("correctly convert time after 12:00", function(){
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
});
test("correctly convert time after 12:00", () =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a pretty thorough set of tests - well done!


const hours = Number(time.slice(0, 2));

if (hours > 12) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that in several of your branches you're doing the same thing - writing time.slice(-2) - if you had to change that for some reason, you'd need to change both copies. Can you think how to avoid this duplication?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted: minutes is worked out once on line 3.

} else if (hours === 12) {
return `${time} pm`;
} else if (hours === 0) {
return `12:${time.slice(-2)} am`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed that all of these branches have something in common - at its core, all of them are calculating an "hours", calculating a "minutes", and appending an "am" or "pm"

Often it can be useful to make clear in code what things are the same and what things are different. Can you think how you may structure this code so that you always just return ${hours}:${minutes} am/pm, but make clear with your if statement how you're differently computing those things?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer now, with one return and the ifs only deciding the hour and the period.

@illicitonion illicitonion added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Sep 28, 2026
@habohlin habohlin added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 3, 2026

@abdishakoor-dev abdishakoor-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactor reads well now: minutes is worked out once, the two if statements only decide the hour and the period, and there is a single return. I checked it against midnight, noon and the minutes either side of both, and every one comes out right. Two things before I can mark it Complete:

  1. timeConverter.test.js: the exact boundaries aren't tested yet. See the comment on line 23.
  2. timeConverter.js fails Prettier again since the last commit (spaces before {, around : and -, and a missing semicolon on line 22). You formatted it once already in an earlier commit, so turning on format on save will stop this coming back: https://github.com/CodeYourFuture/Module-JavaScript-Fundamentals/blob/main/practical_guide.md

Add the Needs Review label again once you've pushed.

test("can format early noon with minutes other than 00", () =>
assert.equal(formatAs12HourClock("12:17"), "12:17 pm"));

test("can format time between midnight and 1 am", () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

00:15 checks the midnight hour, but not midnight itself. Which input is the very first minute of the day, which are the last minute before noon and the last minute before midnight, and which is the first time your hours >= 13 branch handles? Those are the places an if on hours is most likely to go wrong, so each is worth its own test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All four are covered now on lines 26-36, thanks.

if (hours >= 13){
hourString = hours - 12 < 10 ? `0${hours - 12}` : `${hours -12}`;
} else if (hours === 0){
hourString = `${hours + 12}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: when hours is 0, what is hours + 12 always going to be? Would writing that value directly make this branch easier to read?

@abdishakoor-dev abdishakoor-dev added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 4, 2026
@habohlin habohlin added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 4, 2026

@abdishakoor-dev abdishakoor-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both points from last time are done: the four boundary tests (00:00, 11:59, 23:59, 13:00) are in and pass, and both files are Prettier-clean now. I ran the function on midnight, noon and the minute either side of each, and every one is right. Writing each new test in its own commit with whether it passed made the history easy to follow.

Marking this Complete.

@abdishakoor-dev abdishakoor-dev added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants